#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "memberschedule.h"
#include "schedule.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QStackedWidget>
#include <QDir>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QLabel>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    initLog();
    QLOG_INFO() << "Program started.";
    // MainWindow的初始化
    initMainWindow();

    // 创建并初始化stacked widget
    initStackWidget();
}

MainWindow::~MainWindow()
{
    delete ui;
    deinitLog();
}

void MainWindow::initLog()
{
    // 初始化日志机制
    Logger& logger = Logger::instance();

    // 日志等级 >=设置等级的才会被打印
    logger.setLoggingLevel(LOGLEVEL);

    // 创建log文件夹
    QDir folder;
    const QString sLogDir = QApplication::applicationDirPath() + "/logs";
    if(false == folder.exists(sLogDir)){
        folder.mkdir(sLogDir);
    }

    // 添加文件为目的地
    const QString sLogPath = sLogDir + "/log.txt";
    // 2M大小
    DestinationPtr fileDestination(DestinationFactory::MakeFileDestination(
      sLogPath, EnableLogRotation, MaxSizeBytes(512 * 4 * 1024), MaxOldLogCount(5)));
    logger.addDestination(fileDestination);

    return;
}

void MainWindow::deinitLog()
{
    QLOG_INFO() << "Program exited.";
    QsLogging::Logger::destroyInstance();
    return;
}

void MainWindow::initMainWindow(void)
{
    // 初始化成员变量
    pStackWidget = NULL;

    this->resize(WINDOW_WIDTH, WINDOW_HEIGHT);

    // 初始化MainWindow界面
    setWindowTitle(SOFTWARE_NAME);
}

void MainWindow::initStackInnerWidget()
{
    for(int i = 0; i < MAX_STACKWIDGET_PAGE_COUNT; i++)
        frameWidgetArray[i] = new QWidget;

    QWidget * frameWidget = new QWidget(this);
    this->setCentralWidget(frameWidget);

    QVBoxLayout *myLayout = new QVBoxLayout();
    myLayout->addWidget(pStackWidget);
    frameWidget->setLayout(myLayout);

    return;
}

void MainWindow::slot_myStackedWidget_currentChanged(int index)
{
    if(STACKWIDGET_DATE_INDEX == index){
        DateSet * pDateSet = (DateSet *)widgetArray[STACKWIDGET_DATE_INDEX];
        if(NULL == pDateSet)
            return;
        pDateSet->slot_dateSet_start();
    } else if(STACKWIDGET_SCHEDULE_INDEX == index){
        Schedule * pTmp =  (Schedule *)widgetArray[STACKWIDGET_SCHEDULE_INDEX];
        if(NULL == pTmp)
            return;
        pTmp->clearData();
        pTmp->slot_Schedule_start();
    }

    return;
}

void MainWindow::initStackWidget(void)
{
    pStackWidget = new QStackedWidget(this);

    initStackInnerWidget();

    // 设置stackWidget中的各界面
    initStackWidgetPage();

    connect(pStackWidget, QOverload<int>::of(&QStackedWidget::currentChanged), this, &MainWindow::slot_myStackedWidget_currentChanged);
    return;
}

void MainWindow::initStackWidgetPage(void)
{
    // 开始界面的widget
    widgetArray[STACKWIDGET_WELCOME_INDEX] = new QWidget(frameWidgetArray[STACKWIDGET_WELCOME_INDEX]);
    pStackWidget->addWidget(frameWidgetArray[STACKWIDGET_WELCOME_INDEX]);
    initWelcomePage();

    // 人员设置界面的widget
    widgetArray[STACKWIDGET_MEMBERSCHEDULE_INDEX] = new MemberSchedule(frameWidgetArray[STACKWIDGET_MEMBERSCHEDULE_INDEX], pStackWidget);
    pStackWidget->addWidget(frameWidgetArray[STACKWIDGET_MEMBERSCHEDULE_INDEX]);

    // 日期设置界面的widget
    widgetArray[STACKWIDGET_DATE_INDEX] = new DateSet(frameWidgetArray[STACKWIDGET_DATE_INDEX], pStackWidget);
    pStackWidget->addWidget(frameWidgetArray[STACKWIDGET_DATE_INDEX]);

    // 排班界面的widget
    widgetArray[STACKWIDGET_SCHEDULE_INDEX] = new Schedule(frameWidgetArray[STACKWIDGET_SCHEDULE_INDEX], pStackWidget);
    pStackWidget->addWidget(frameWidgetArray[STACKWIDGET_SCHEDULE_INDEX]);

    return;
}

void MainWindow::initWelcomePage()
{
    QPushButton * btn = new QPushButton("开始", frameWidgetArray[STACKWIDGET_WELCOME_INDEX]);
    btn->setStyleSheet(WELCOMEWIDGET_PUSHBUTTON_STYLE);
    btn->setFixedSize(165, 80);
    connect(btn, &QPushButton::clicked, this, [=](){pStackWidget->setCurrentIndex(STACKWIDGET_MEMBERSCHEDULE_INDEX);});

    QLabel * plabel1 = new QLabel("源码下载地址:", frameWidgetArray[STACKWIDGET_WELCOME_INDEX]);
    QLabel * plabel2 = new QLabel("<a href='https://gitee.com/she_10086/Scheduling.git'>https://gitee.com/she_10086/Scheduling.git</a>", frameWidgetArray[STACKWIDGET_WELCOME_INDEX]);
    plabel2->setOpenExternalLinks(true);

    // 设置布局
    QHBoxLayout * myLayout = new QHBoxLayout;
    myLayout->addSpacerItem(new QSpacerItem(40, 20,QSizePolicy::Expanding,QSizePolicy::Expanding));
    myLayout->addWidget(btn);
    myLayout->addSpacerItem(new QSpacerItem(40, 20,QSizePolicy::Expanding,QSizePolicy::Expanding));

    QHBoxLayout * myLayout2 = new QHBoxLayout;
    myLayout2->addStretch(11);
    myLayout2->addWidget(plabel1);
    myLayout2->addWidget(plabel2);
    myLayout2->addStretch(1);

    QVBoxLayout * myLayout3 = new QVBoxLayout;
    myLayout3->addLayout(myLayout);
    myLayout3->addLayout(myLayout2);

    frameWidgetArray[STACKWIDGET_WELCOME_INDEX]->setLayout(myLayout3);
    frameWidgetArray[STACKWIDGET_WELCOME_INDEX]->setStyleSheet("background-color: rgba(123, 139, 111)");
    return;
}

void MainWindow::deinitFrameWidgetArray()
{
    for (int i = 0; i < MAX_STACKWIDGET_PAGE_COUNT; i++)
        if(NULL != frameWidgetArray[i])
            delete frameWidgetArray[i];

    return ;
}
